﻿using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraTreeList.Columns;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.OpenCode.Framework.DevForm
{
    public delegate void ShowCheckBoxesChangedHandler(bool ShowCheckBoxes);

    public class OpenCodeTreeList : OpenCodeTreeUI
    {
        /// <summary>
        /// 复选框变化事件。
        /// </summary>
        public event ShowCheckBoxesChangedHandler ShowCheckBoxesChangedEvent;

        public OpenCodeTreeList()
        {
            this.OptionsView.ShowColumns = false;
            this.OptionsView.ShowVertLines = false;
            this.OptionsView.ShowHorzLines = false;
        }

        private bool _showCheckBoxes = false;
        /// <summary>
        /// 获取或设置树是否显示复选框。
        /// </summary>
        public bool ShowCheckBoxes
        {
            get { return this.OptionsView.ShowCheckBoxes; }
            set
            {
                _showCheckBoxes = value;
                this.OptionsView.ShowCheckBoxes = _showCheckBoxes;
                if (ShowCheckBoxesChangedEvent != null)
                {
                    ShowCheckBoxesChangedEvent(_showCheckBoxes);
                }
            }
        }

        protected override void OnLoaded()
        {
            base.OnLoaded();
        }

        public override void CreateColumn(string name, bool visible = true, int width = 100)
        {
            base.CreateColumn(name, visible, width);
            this.Columns[name].MinWidth = 20;
            this.Columns[name].OptionsColumn.AllowMove = true;
            this.Columns[name].Width = width;
        }

        public virtual RepositoryItemButtonEdit CreateRepositoryItemButtonEditColumn(string caption, int width)
        {
            DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit repositoryItemButtonEdit = new DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit();
            repositoryItemButtonEdit.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Simple;
            repositoryItemButtonEdit.Buttons.Clear();
            repositoryItemButtonEdit.TextEditStyle = TextEditStyles.HideTextEditor;
            EditorButton btnDo = new EditorButton { Kind = ButtonPredefines.Glyph };
            btnDo.Caption = caption;
            repositoryItemButtonEdit.Buttons.Add(btnDo);
            TreeListColumn col = new TreeListColumn();
            col.ColumnEdit = repositoryItemButtonEdit;
            col.Visible = true;
            col.VisibleIndex = this.Columns.Count;
            col.Width = width;
            this.RepositoryItems.AddRange(new DevExpress.XtraEditors.Repository.RepositoryItem[] {
            repositoryItemButtonEdit});
            this.Columns.AddRange(new TreeListColumn[] { col });
            return repositoryItemButtonEdit;
        }

        public virtual RepositoryItemButtonEdit CreateRepositoryItemButtonEdit(string caption)
        {
            DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit repositoryItemButtonEdit = new DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit();
            repositoryItemButtonEdit.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Simple;
            repositoryItemButtonEdit.Buttons.Clear();
            repositoryItemButtonEdit.TextEditStyle = TextEditStyles.HideTextEditor;
            EditorButton btnDo = new EditorButton { Kind = ButtonPredefines.Glyph };
            btnDo.Caption = caption;
            repositoryItemButtonEdit.Buttons.Add(btnDo);
            this.RepositoryItems.AddRange(new DevExpress.XtraEditors.Repository.RepositoryItem[] {
            repositoryItemButtonEdit});
            return repositoryItemButtonEdit;
        }

        public virtual RepositoryItemComboBox CreateRepositoryItemComboBox(string caption, int columnWidth)
        {
            DevExpress.XtraEditors.Repository.RepositoryItemComboBox repositoryItemTableTypeCom = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
            repositoryItemTableTypeCom.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            TreeListColumn col = new TreeListColumn();
            col.ColumnEdit = repositoryItemTableTypeCom;
            col.Visible = true;
            col.Caption = caption;
            col.VisibleIndex = this.Columns.Count;
            col.Width = columnWidth;
            this.RepositoryItems.AddRange(new DevExpress.XtraEditors.Repository.RepositoryItem[] {
            repositoryItemTableTypeCom});
            this.Columns.AddRange(new TreeListColumn[] { col });
            return repositoryItemTableTypeCom;
        }
    }
}
